package com.huike.web.controller.clues;


import com.github.pagehelper.Page;
import com.huike.business.service.TbClueService2;
import com.huike.clues.domain.dto.ClueAssignmentDTO;
import com.huike.clues.domain.dto.TbClueAddDTO;
import com.huike.clues.domain.dto.TbCluePageDTO;
import com.huike.clues.domain.dto.TbCluePoolPageDTO;
import com.huike.clues.domain.vo.CluePageVo;
import com.huike.clues.service.TbClueService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.result.PageResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 线索管理Controller
 *
 * @date 2023-04-02
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/clues/clue")
public class TbClueController {
    private final TbClueService tbClueService;

    private final TbClueService2 tbClueService2;

    @GetMapping("/list")
    public PageResult findClueList(TbCluePageDTO tbCluePageDTO) {
        log.info("执行分页查询线索{}", tbCluePageDTO);
        LocalDate beginCreateTime = null;
        LocalDate endCreateTime = null;
        if (tbCluePageDTO.getParams().get("beginCreateTime") != "" &&
                tbCluePageDTO.getParams().get("endCreateTime") != "") {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String beginCreateTimeStr = (String) tbCluePageDTO.getParams().get("beginCreateTime");
            beginCreateTime = LocalDate.parse(beginCreateTimeStr, formatter);

            String endCreateTimeStr = (String) tbCluePageDTO.getParams().get("endCreateTime");
            endCreateTime = LocalDate.parse(endCreateTimeStr, formatter);
        }
        Page<CluePageVo> page = tbClueService.findClueList(tbCluePageDTO, beginCreateTime, endCreateTime);
        return PageResult.success(page.getTotal(), page.getResult());
    }

    @PostMapping
    public AjaxResult clueAdd(@RequestBody TbClueAddDTO tbClueAddDTO) {
        log.info("执行了添加线索接口{}", tbClueAddDTO);
        tbClueService.ClueAdd(tbClueAddDTO);
        return AjaxResult.success("添加成功");
    }

    @PutMapping("/assignment")
    public AjaxResult clueAssignment(@RequestBody ClueAssignmentDTO clueAssignmentDTO) {
        log.info("执行了批量分配接口,被分配的id:{},分配给{}",clueAssignmentDTO.getIds(),clueAssignmentDTO.getUserId());
        tbClueService.clueAssignment(clueAssignmentDTO);
        return AjaxResult.success("批量修改成功");
    }

    @GetMapping("/{id}")
    public AjaxResult<CluePageVo> ClueAssign(@PathVariable Long id){
        log.info("查看跟进信息的线索的ID:{}",id);
        CluePageVo clueAssignVo = tbClueService.ClueAssign(id);
        return AjaxResult.success(clueAssignVo);
    }
    @PutMapping("/changeBusiness/{id}")
    public AjaxResult clueChangeBusiness(@PathVariable Long id) {
        log.info("执行线索转商机接口,转化的线索ID为:{}",id);
        tbClueService2.clueChangeBusiness(id);
        return AjaxResult.success("转化商机成功");
    }

    @PutMapping("/false/{id}")
    public AjaxResult falseClue(@PathVariable Long id, String reason, String remark){
        log.info("执行线索转为伪线索,id{},原因:{},说明:{}",id, reason, remark);
        tbClueService.falseClue(id,reason,remark);
        return AjaxResult.success("设为伪线索成功");
    }

    @PutMapping("/gain")
    public AjaxResult clueGain(@RequestBody ClueAssignmentDTO clueAssignmentDTO){
        log.info("执行捞取接口,捞取的线索id:{}",clueAssignmentDTO.getIds());
        tbClueService.clueGain(clueAssignmentDTO);
        return AjaxResult.success("捞取成功");
    }

    @GetMapping("/pool")
    public PageResult findCluePoolList(TbCluePoolPageDTO tbCluePoolPageDTO){
        log.info("执行分页查询线索池{}", tbCluePoolPageDTO);
        LocalDate beginCreateTime = null;
        LocalDate endCreateTime = null;
        if (tbCluePoolPageDTO.getParams().get("beginCreateTime") != "" &&
                tbCluePoolPageDTO.getParams().get("endCreateTime") != "") {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String beginCreateTimeStr = (String) tbCluePoolPageDTO.getParams().get("beginCreateTime");
            beginCreateTime = LocalDate.parse(beginCreateTimeStr, formatter);

            String endCreateTimeStr = (String) tbCluePoolPageDTO.getParams().get("endCreateTime");
            endCreateTime = LocalDate.parse(endCreateTimeStr, formatter);
        }
        Page<CluePageVo> page = tbClueService.findCluePoolList(tbCluePoolPageDTO, beginCreateTime, endCreateTime);
        return PageResult.success(page.getTotal(), page.getResult());

    }
}
